﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using Simple.Amazon.ECS;

namespace AmazonWebService {
	public class AmazonWSBook {
		// OWLS Amazon ID's
		private const string accessKeyId	= "AKIAIQI63SOEMBVHODIQ";
		private const string secretKey		= "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";
//		private const string ISBN	= "9780590353403";
		
		private string		isbn;
		private string		title;
		private string[]	author;
		private string		publisher;
		private string		genre;
		private string		shortSynopsis;
		private Image		largeImage;
		
		public AmazonWSBook (Item item) {
			isbn = item.ItemAttributes.ISBN;
			title = item.ItemAttributes.Title;
			author = item.ItemAttributes.Author;
			publisher = item.ItemAttributes.Publisher;
			genre = item.ItemAttributes.Genre;
			shortSynopsis = item.ItemAttributes.ShortSynopsis;
			largeImage = item.LargeImage;
		}
		
		public string ISBN {
			get {
				return isbn;
			}
		}
		
		public string Title {
			get {
				return title;
			}
		}
		
		public string[] Author {
			get {
				return author;
			}
		}
		
		public string Publisher {
			get {
				return publisher;
			}
		}
		
		public string Genre {
			get {
				return genre;
			}
		}
		
		public string ShortSynopsis {
			get {
				return shortSynopsis;
			}
		}
		
		public Image LargeImage {
			get {
				return largeImage;
			}
		}
		
		private static List<AmazonWSBook> SearchBooks(ItemSearchRequest request) {
			List<AmazonWSBook> books = new List<AmazonWSBook>(0);

			// create a WCF Amazon ECS client
			BasicHttpBinding binding		= new BasicHttpBinding(BasicHttpSecurityMode.Transport);
			binding.MaxReceivedMessageSize	= int.MaxValue;
			AWSECommerceServicePortTypeClient client = new AWSECommerceServicePortTypeClient(
				binding,
				new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));

			// add authentication to the ECS client
			client.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior(accessKeyId, secretKey));
			
			// finallize request
			request.SearchIndex			= "Books";
			request.ResponseGroup 		= new string[] { "Medium" };

			ItemSearch itemSearch		= new ItemSearch();
			itemSearch.Request			= new ItemSearchRequest[] { request };
			itemSearch.AWSAccessKeyId	= accessKeyId;
			
			// issue the ItemSearch request
			ItemSearchResponse response = client.ItemSearch(itemSearch);
			
			// write out the results
			foreach (var item in response.Items[0].Item) {
				books.Add(new AmazonWSBook(item));
			}
			return books;
		}
		
		public static AmazonWSBook GetBookByISBN(string isbn) {
			// prepare an ItemSearch request for isbn
			ItemSearchRequest request	= new ItemSearchRequest();
	        request.Power 				= "ISBN:" + isbn.Trim();
			request.ItemPage			= "1";
			return SearchBooks(request)[0];
		}
		
		public static List<AmazonWSBook> SearchBooksByTitle(string title) {
			return SearchBooksByTitle(title, 1);
		}
		
		public static List<AmazonWSBook> SearchBooksByTitle(string title, int searchResultPageNo) {
			// prepare an ItemSearch request for title
			ItemSearchRequest request	= new ItemSearchRequest();
			request.Title				= title.Trim();
			request.ItemPage			= Convert.ToString(searchResultPageNo);
			return SearchBooks(request);
		}
		
		public static List<AmazonWSBook> SearchBooksByAuthor(string author) {
			return SearchBooksByAuthor(author, 1);
		}
		
		public static List<AmazonWSBook> SearchBooksByAuthor(string author, int searchResultPageNo) {
			// prepare an ItemSearch request for author
			ItemSearchRequest request	= new ItemSearchRequest();
			request.Author				= author.Trim();
			request.ItemPage			= Convert.ToString(searchResultPageNo);
			return SearchBooks(request);
		}
	}
}
